<template>
  <!-- 列表 -->
  <ContentWrap>
    <vxe-toolbar ref="toolbarRef" custom />
    <vxe-table
      id="iPickBatch-table"
      ref="tableRef"
      v-loading="loading"
      :data="list"
      stripe
      :custom-config="vxeTableConfig"
      :column-config="{ resizable: true }"
      :size="vxeTableSize"
    >
      <vxe-column :title="t('common.index')" type="seq" field="seq" width="60" align="center" />
      <vxe-column
        :title="t('mentorPickApply.fields.batchName')"
        field="batchName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('mentorPickApply.fields.year')"
        field="year"
        align="center"
        show-header-overflow
        show-overflow
        min-width="50"
      />
      <vxe-column
        :title="t('mentorPickApply.fields.applyStartTime')"
        field="applyStartTime"
        align="center"
        show-header-overflow
        show-overflow
        min-width="120"
        :formatter="vxeDateFormatter"
      />
      <vxe-column
        :title="t('mentorPickApply.fields.applyEndTime')"
        field="applyEndTime"
        align="center"
        show-header-overflow
        show-overflow
        min-width="120"
        :formatter="vxeDateFormatter"
      />
      <vxe-column
        :title="t('mentorPickApply.fields.isApply')"
        field="isApply"
        align="center"
        show-header-overflow
        show-overflow
        min-width="60"
      >
        <template #default="{ row }">
          <el-tag v-if="row.isApply" type="success">{{ t('mentorPickApply.status.apply') }}</el-tag>
          <el-tag v-else type="danger">{{ t('mentorPickApply.status.unApply') }}</el-tag>
        </template>
      </vxe-column>
      <vxe-column
        :title="t('mentorPickApply.fields.applyTime')"
        field="applyTime"
        align="center"
        show-header-overflow
        show-overflow
        min-width="120"
        :formatter="vxeDateFormatter"
      />
      <vxe-column
        :title="t('mentorPickApply.fields.reviewResult')"
        field="reviewResult"
        align="center"
        show-header-overflow
        show-overflow
        min-width="60"
      >
        <template #default="{ row }">
          <el-tag v-if="row.isApply && !row.reviewResult" type="info">{{
            t('mentorPickApply.status.unfinished')
          }}</el-tag>
          <el-tag v-else-if="row.isApply && row.reviewResult === 1" type="success">{{
            t('mentorPickApply.status.passed')
          }}</el-tag>
          <el-tag v-else-if="row.isApply && row.reviewResult === -1" type="danger">{{
            t('mentorPickApply.status.unPassed')
          }}</el-tag>
        </template>
      </vxe-column>
      <vxe-column
        :title="t('mentorPickApply.fields.applyTemplateUrl')"
        field="applyTemplateUrl"
        align="center"
        show-header-overflow
        show-overflow
        min-width="60"
      >
        <template #default="{ row }">
          <FileObjectDownloadList
            v-if="row.applyTemplateUrl"
            :file-json-list="row.applyTemplateUrl"
          />
        </template>
      </vxe-column>
      <vxe-column
        :title="t('common.operate')"
        field="operator"
        align="center"
        fixed="right"
        width="200"
      >
        <template #default="{ row }">
          <el-button
            v-if="!row.isApply"
            link
            type="primary"
            :disabled="Date.now() < row.applyStartTime || Date.now() > row.applyEndTime"
            @click="handleClick('apply', row)"
          >
            {{ t('mentorPickApply.operate.apply') }}
          </el-button>
          <el-button v-if="row.isApply" link type="primary" @click="handleClick('view', row)">
            {{ t('mentorPickApply.operate.view') }}
          </el-button>
        </template>
      </vxe-column>
    </vxe-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 申请/查看申请信息弹窗 -->
  <MentorPickApplyDialog ref="mentorPickApplyDialogRef" @success="getList" />
</template>

<script setup lang="ts">
import { vxeDateFormatter } from '@/utils/formatTime'
import type { VxeTablePropTypes, VxeToolbarInstance, VxeTableInstance } from 'vxe-table'
import { useAppStore } from '@/store/modules/app'
import { MentorApplyApi, MentorPickApplyInfoVO } from '@/api/mentorApply'
import MentorPickApplyDialog from './MentorPickApplyDialog.vue'
import FileObjectDownloadList from '@/components/FileObjectDownload/list.vue'

/** 导师遴选申请-已有账号的研究员 */
defineOptions({ name: 'UserMentorPickApply' })

const { t } = useI18n() // 国际化

const toolbarRef = ref<VxeToolbarInstance>()
const tableRef = ref<VxeTableInstance>()
const mentorPickApplyDialogRef = ref<InstanceType<typeof MentorPickApplyDialog>>()

const loading = ref(true) // 列表的加载中
const list = ref<MentorPickApplyInfoVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  batchName: undefined
})

const vxeTableConfig: VxeTablePropTypes.CustomConfig = {
  storage: true,
  mode: 'drawer',
  checkMethod({ column }) {
    return !['seq', 'operator'].includes(column.field)
  }
}

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await MentorApplyApi.getPickBatchList(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

// 点击申请/申请详情
const handleClick = (type: 'apply' | 'view', row: MentorPickApplyInfoVO) => {
  mentorPickApplyDialogRef.value?.open(type, row)
}

/** 初始化 **/
onMounted(() => {
  getList()

  const table = tableRef.value
  const toolbar = toolbarRef.value
  if (table && toolbar) {
    table.connect(toolbar)
  }
})
</script>
